package com.jyh.utils;

import java.sql.*;

/**
 * @Author JYH
 * @Date 2024/8/22 14:20
 * @Description 专门操作数据的一个工具类
 */
public final class JDBCUtil {
    private JDBCUtil() {
    }
    public static final String MYSQL_DRIVER="com.mysql.jdbc.Driver";
    public static final String URL="jdbc:mysql://localhost:3306/school?useUnicode=true&useSSL=false&characterEncoding=UTF8";
    public static final String USERNAME="root";
    public static final String PASSWORD="123456";
    static{
        try {
            Class.forName(MYSQL_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //1.获取连接对象的方法
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //2.关闭资源
    public static void close(Statement statement,Connection connection){
        if (statement!=null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //3.关闭资源
    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
